package com.igeek.collateral.controller;

import cn.hutool.core.collection.CollectionUtil;
import com.igeek.collateral.dto.ChildDto;
import com.igeek.collateral.dto.FatherDto;
import com.igeek.collateral.entity.Menu;
import com.igeek.collateral.entity.Meta;
import com.igeek.collateral.result.Result;
import com.igeek.collateral.service.IMenuService;
import com.igeek.collateral.vo.MenuTableVo;
import com.igeek.collateral.vo.MenuVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/menu")
@Api(tags = "菜单接口")
@Slf4j
public class MenuController {
    @Autowired
    private IMenuService menuService;
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;




    @GetMapping("getMenu")
    public Result getMenu() {
        List<Menu> list = menuService.list();
        if (list != null && list.size() > 0) {
            return new Result(true, "获取菜单成功", list);
        }
        return new Result( false, "获取菜单失败") ;
    }


    @PostMapping("addMenu")
    public Result addMenu(@RequestBody Menu menu) {
        boolean save = menuService.save(menu);
        if (save) {
            return new Result(true, "添加菜单成功");
        }
        return new Result(false, "添加菜单失败");
    }


    @ApiOperation("实现动态菜单信息")
    @GetMapping("findAll")
    public Result findAll(){
        List<MenuVo> menuList = menuService.findAll();
        if(!CollectionUtils.isEmpty(menuList)){
            return new Result(true , "查询菜单成功", menuList);
        }
        return new Result(false , "查询菜单信息失败" );
    }


    @ApiOperation("查询所有菜单信息")
    @GetMapping("findAllMenu")
    public Result findAllMenu(){
        List<MenuTableVo> menuList = menuService.findAllMenu();
        if(!CollectionUtils.isEmpty(menuList)){
            return new Result(true , "查询菜单成功", menuList);
        }
        return new Result(true , "查询菜单信息失败" );
    }

    //新增菜单
    @PostMapping("addF")
    @ApiOperation("新增一级菜单接口")
    public Result add(@RequestBody FatherDto fatherDto){

        boolean flag = menuService.addF(fatherDto);
        if (flag){
            return new Result(true,"增加成功");
        }
        return new Result(false,"增加失败");
    }


    @PostMapping("addS")
    @ApiOperation("新增二级菜单接口")
    public Result addS(@RequestBody ChildDto childDto, @RequestParam(required = false,name="fatherMenuId") Integer fatherMenuId){
        boolean flag = menuService.addS(childDto, fatherMenuId);
        if (flag){
            return new Result(true,"增加成功");
        }
        return new Result(false,"增加失败");
    }
    @GetMapping("getFatherMenu")
    @ApiOperation("查询一级菜单接口")
    public Result getFatherMenu(){
        List<Menu> list = menuService.selectFather();
        if (list != null && list.size() > 0) {
            return new Result(true, "获取菜单成功", list);
        }
        return new Result( false, "获取菜单失败") ;
    }

    @PutMapping
    @ApiOperation("修改菜单接口")
    public Result updateMenu(@RequestBody ChildDto childDto, @RequestParam(required = false,name="fatherMenuId") Integer fatherMenuId){
        boolean flag = menuService.upadateSonMenu(childDto, fatherMenuId);
        if (flag){
            return new Result(true,"更新子菜单成功");
        }
        return new Result(false,"更新子菜单失败");
    }


    @PutMapping("updateF")
    @ApiOperation("修改菜单接口")
    public Result updateMenu(@RequestBody FatherDto fatherDto){
        boolean flag = menuService.upadateFatherMenu(fatherDto);
        if (flag){
            return new Result(true,"更新父菜单成功");
        }
        return new Result(false,"更新父菜单失败");
    }

    @GetMapping("{id}")
    @ApiOperation("根据id查询菜单信息")
    public Result findOne(@PathVariable("id") Integer id) {
        Menu menu= menuService.getById(id);
        if(menu != null){
            return new Result(true , "通过id查询菜单信息成功" ,menu);
        }
        return new Result(true , "通过id查询菜单信息失败");
    }


    @DeleteMapping("{id}")
    @ApiOperation("根据id删除菜单信息")
    public Result deleteById(@PathVariable("id") Integer id) {
        boolean flag = menuService.deleteById(id);
        if (flag) {
            return new Result(true, "删除菜单信息成功");
        }
        return new Result(false, "删除菜单信息失败");
    }











}
